package com.shilei;

import com.shilei.common.Dir;
import com.shilei.common.Group;
import com.shilei.common.ResourceMgr;

import java.awt.*;

/**
 * @description: 炮弹类
 * @author: 81271
 * @date: 2020-04-07 21:43
 */
public class Bullet extends GameObject{
    public static int HEIGHT = ResourceMgr.bulletD.getHeight();
    public static int WIDTH = ResourceMgr.bulletD.getWidth();
    private static final int SPEED = 20;


    private Dir dir;
    private Group group;

    private Rectangle rect = new Rectangle();

    private Boolean liveBl = true;

    public Bullet(int x, int y, Dir dir, Group group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;

        rect.x = this.x;
        rect.y = this.y;
        rect.width = WIDTH;
        rect.height = HEIGHT;

        GameModel.getInstance().gameObjectList.add(this);
    }


    public void paint(Graphics g) {
        if (!liveBl) {
            GameModel.getInstance().gameObjectList.remove(this);
        }

        g.setColor(Color.RED);
        switch (dir) {
            case LEFT:
                g.drawImage(ResourceMgr.bulletL, x, y, null);
                break;
            case RIGHT:
                g.drawImage(ResourceMgr.bulletR, x, y, null);
                break;
            case UP:
                g.drawImage(ResourceMgr.bulletU, x, y, null);
                break;
            case DOWN:
                g.drawImage(ResourceMgr.bulletD, x, y, null);
                break;
        }
        move();
    }

    private void move() {
        switch (dir) {
            case RIGHT:
                x += SPEED;
                break;
            case LEFT:
                x -= SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
        }

        rect.x = this.x;
        rect.y = this.y;

        if (x < 0 || y < 0|| x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) {
            this.die();
        }
    }


    public void die() {
        liveBl = false;
    }

    public Group getGroup() {
        return group;
    }

    public Rectangle getRect() {
        return rect;
    }

    @Override
    public int getHeight() {
        return HEIGHT;
    }

    @Override
    public int getWidth() {
        return WIDTH;
    }
}
